package cn.dckeji.dcssm.service.impl;

import cn.dckeji.dcssm.common.enums.ProjectEnum;
import cn.dckeji.dcssm.dao.AdminDao;
import cn.dckeji.dcssm.dao.MenuDao;
import cn.dckeji.dcssm.dao.RoleMenuDao;
import cn.dckeji.dcssm.entity.Admin;
import cn.dckeji.dcssm.entity.Menu;
import cn.dckeji.dcssm.entity.RoleMenu;
import cn.dckeji.dcssm.service.MenuService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author Kenny
 * @since 2019-02-25
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuDao, Menu> implements MenuService {

    @Resource
    private MenuDao menuDao;
    @Resource
    private RoleMenuDao roleMenuDao;
    @Resource
    private AdminDao adminDao;

    /**
     * 查询后台菜单
     * @return
     * @param adminId
     */
    @Override
    public Map getMenu(String adminId) {
        Admin admin = adminDao.selectById(adminId);
        String roleId = admin.getRoleId();
        QueryWrapper<RoleMenu> roleMenuQueryWrapper = new QueryWrapper<>();
        roleMenuQueryWrapper.eq("role_id", roleId);
        List<RoleMenu> roleMenuList = roleMenuDao.selectList(roleMenuQueryWrapper);

        List<String> menuIdList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(roleMenuList)) {
            for (RoleMenu rm: roleMenuList) {
                String menuId = rm.getMenuId();
                menuIdList.add(menuId);
            }
        }
        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
        menuQueryWrapper.eq("pid", ProjectEnum.MENU_PID_DEFAULT.code());
        menuQueryWrapper.orderBy(true,false,"order_num");
        List<Menu> menuList = menuDao.selectList(menuQueryWrapper);

        List<Map> menuMapList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(menuList)) {
            int size = menuList.size();
            for (int i = 0; i < size; i++) {

                Menu topMenu = menuList.get(i);
                String topMenuId = topMenu.getId();
                if (menuIdList.contains(topMenuId)) {
                    Map<String, Object> stringObjectMap = BeanUtil.beanToMap(topMenu);
                    stringObjectMap.put("spread", false);

                    //查询子菜单
                    menuQueryWrapper=new QueryWrapper<>();
                    menuQueryWrapper.eq("pid", topMenuId);
                    menuQueryWrapper.orderBy(true,false,"order_num");
                    List<Menu> kidMenuList = menuDao.selectList(menuQueryWrapper);

                    List<Map> kidMenuMapList = new ArrayList<>();
                    if (CollectionUtil.isNotEmpty(kidMenuList)) {
                        int kidMenuSize = kidMenuList.size();
                        for (int j = 0; j < kidMenuSize; j++) {
                            Menu kidMenu = kidMenuList.get(j);
                            String kidMenuId = kidMenu.getId();
                            if (menuIdList.contains(kidMenuId)) {
                                Map<String, Object> stringObjectMap1 = BeanUtil.beanToMap(kidMenu);
                                stringObjectMap1.put("spread", false);
                                kidMenuMapList.add(stringObjectMap1);
                            }
                        }
                        stringObjectMap.put("children", kidMenuMapList);
                    }
                    menuMapList.add(stringObjectMap);
                }
            }
        }
        Map map = new HashMap();
        map.put("contentManagement", menuMapList);
        return map;
    }

    @Override
    public void deleteById(String menuId) {
        Menu menu = menuDao.selectById(menuId);
        String pid = menu.getPid();
        if ("-1".equals(pid)) {
            //如果是一级菜单,把子菜单也删掉
            List<Menu> menuList = menuDao.selectList(new QueryWrapper<Menu>().eq("pid", menuId));
            if (CollectionUtil.isNotEmpty(menuList)) {
                for (Menu kidm:menuList) {
                    String kidmId = kidm.getId();
                    Map map = new HashMap();
                    map.put("menu_id", kidmId);
                    roleMenuDao.deleteByMap(map);
                    menuDao.deleteById(kidmId);
                }
            }
        }
        Map map = new HashMap();
        map.put("menu_id", menuId);
        roleMenuDao.deleteByMap(map);
        menuDao.deleteById(menuId);
    }
}
